﻿package com.tudou.band
{
    import com.tudou.log.*;
    import com.tudou.utils.*;
    import flash.net.*;
    import flash.utils.*;

    public class BandManager extends Object
    {
        public var bandCtrl:Number = 0.1;
        private var lastPausedTime:int = 20;
        private var highTime:int = -1;
        private var userPauseBufferTime:int = -1;
        private var lastSend:int;
        private static const MIN_DURATION:uint = 350;
        private static const PRESERVE_DURATION:uint = 180;
        private static var lock:Boolean = true;
        private static var instance:BandManager = null;
        private static const SEND_INTERVAL:uint = 5000;
        private static const PAUSE_TIME:uint = 20;

        public function BandManager()
        {
            lastSend = -SEND_INTERVAL;
            if (!lock)
            {
            }
            else
            {
                throw new Error("This is a singleton class BandManager!");
            }
            return;
        }// end function

        public function setPlayTime(param1:int) : void
        {
            if (param1 > highTime)
            {
                highTime = param1;
            }
            return;
        }// end function

        private function judgeTime() : Boolean
        {
            var _loc_1:* = getTimer();
            if (_loc_1 - lastSend > SEND_INTERVAL)
            {
                return true;
            }
            return false;
        }// end function

        private function send(param1:uint, param2:String, param3:int, param4:int, param5:int, param6:int) : void
        {
            var reg:RegExp;
            var a:Array;
            var pauseUrl:String;
            var request:URLRequest;
            var saveTime:* = param1;
            var url:* = param2;
            var started:* = param3;
            var highPlayed:* = param4;
            var loaded:* = param5;
            var totalTime:* = param6;
            if (lastPausedTime == 0 && saveTime == 0)
            {
                return;
            }
            if (judgeTime() || lastPausedTime != saveTime)
            {
                lastPausedTime = saveTime;
                reg = /(http:\/\/[^\/]*\/)([^\?]*).+&posky=([^&]*)""(http:\/\/[^\/]*\/)([^\?]*).+&posky=([^&]*)/ig;
                a = reg.exec(url);
                pauseUrl = a[1] + "pos?";
                pauseUrl = pauseUrl + ("f=" + a[2]);
                pauseUrl = pauseUrl + ("&k=" + a[3]);
                pauseUrl = pauseUrl + ("&e=" + RandString.noCache);
                pauseUrl = pauseUrl + ("&s=" + lastPausedTime);
                request = new URLRequest(pauseUrl);
                TudouLogger.debug(this, "url:" + pauseUrl, getTimer());
                try
                {
                    sendToURL(request);
                }
                catch (e:Error)
                {
                    TudouLogger.debug(this, e.message);
                }
                lastSend = getTimer();
            }
            return;
        }// end function

        public function reset() : void
        {
            highTime = -1;
            userPauseBufferTime = -1;
            return;
        }// end function

        public function judge(param1:Boolean, param2:Boolean, param3:String, param4:int, param5:int, param6:int, param7:int, param8:Boolean = false) : void
        {
            if (param7 - param6 <= 3 || param7 <= MIN_DURATION || bandCtrl == 0)
            {
                return;
            }
            setPlayTime(param5);
            if (param1 && !param2 && !param8)
            {
                userPauseBufferTime = param6 - highTime;
                send(0, param3, param4, highTime, param6, param7);
            }
            else if (param6 - highTime > userPauseBufferTime && param6 - param4 > PRESERVE_DURATION && judgeByFormula(param4, highTime, param6, param7))
            {
                send(PAUSE_TIME, param3, param4, highTime, param6, param7);
            }
            else
            {
                send(0, param3, param4, highTime, param6, param7);
            }
            return;
        }// end function

        private function judgeByFormula(param1:int, param2:int, param3:int, param4:int) : Boolean
        {
            var _loc_5:* = bandCtrl * param4;
            return param3 - param2 > _loc_5;
        }// end function

        public static function get inst() : BandManager
        {
            if (instance == null)
            {
                lock = false;
                instance = new BandManager;
                lock = true;
            }
            return instance;
        }// end function

    }
}
